<template>
	<view class="container">
		<!-- 空白页 -->
		<view v-if="!hasLogin || empty===true" class="empty">
			<image src="/static/mixmall/emptyCart.jpg" mode="aspectFit"></image>
			<view v-if="hasLogin" class="empty-tips">
				空空如也
				<navigator class="navigator" v-if="hasLogin" url="../index/index" open-type="switchTab">随便逛逛>
				</navigator>
			</view>
			<view v-else class="empty-tips">
				空空如也
				<view class="navigator" @click="navToLogin">去登陆></view>
			</view>
		</view>
		<view v-else>
			<!-- 列表 -->
			<view class="cart-list">
				<block v-for="(item, index) in cartList" :key="item.gid">
					<view class="cart-item" @click="navToDetailPage(item.goodsId)">
						<!-- 	<view class="store">
							<uni-icons type="shop"></uni-icons>
							<text class="store-name">{{item.stoneName}}</text>
						</view> -->
						<view class="selected-item" @click.stop="check('item', index)">
							<image class="selected-item-box"
								:src="item.checked?'/static/mixmall/selected.png':'/static/mixmall/select.png'"
								mode="aspectFit"></image>
						</view>
						<view class="image-wrapper">
							<image :src="item.goodsImagePath" :class="[item.loaded]" mode="aspectFill" lazy-load
								@load="onImageLoad('cartList', index)" @error="onImageError('cartList', index)"></image>
						</view>
						<view class="item-right">
							<text class="title">{{item.goodsName}}</text>
							<!-- <text class="attr">{{item.attr_val}}</text> -->
							<text class="price">¥{{item.price}}</text>
							<view class="step" @click.stop="">
								<uni-number-box :value="item.count>item.inventory?item.inventory:item.count" :min="1"
									:max="item.inventory" :isMax="item.count>=item.inventory?true:false"
									:isMin="item.count===1" :index="index" @change="numberChange($event,index)">
								</uni-number-box>
							</view>
						</view>
						<text class="del-btn yticon icon-fork" @click.stop="deleteCartItem(index)"></text>
					</view>
				</block>
			</view>
			<!-- 底部菜单栏 -->
			<view class="action-section">
				<view class="checkbox">
					<image :src="allChecked?'/static/mixmall/selected.png':'/static/mixmall/select.png'"
						mode="aspectFit" @click="check('all')"></image>
					<view class="clear-btn" :class="{show: allChecked}" @click="clearCart">
						清空
					</view>
				</view>
				<view class="total-box">
					<text class="price">¥{{total}}</text>
					<text class="coupon">
						已优惠
						<text>0</text>
						元
					</text>
				</view>
				<button type="primary" class="no-border confirm-btn" @click="createOrder">去结算</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';

	export default {
		data() {
			return {
				total: 0, //总价格
				allChecked: false, //全选状态  true|false
				empty: false, //空白页现实  true|false
				cartList: [],
			};
		},
		onLoad() {
			this.loadData();
		},
		onShow() {

		},
		watch: {
			//显示空白页
			cartList(e) {
				let empty = e.length === 0 ? true : false;
				if (this.empty !== empty) {
					this.empty = empty;
				}
			}
		},
		computed: {
			...mapState(['hasLogin'])
		},
		methods: {
			//请求数据
			async loadData() {

				let res = await this.helper.requestAPI(this.api.cart);
				let groups = res.body.cartDto.groups;
				let id = 1;
				let entrys = [];
				this.cartList = [];
				
				groups.forEach(item => {
					entrys = entrys.concat(item.entrys);
				})
				
				let list = entrys.map(item => {
					item.gid = id;
					id++;
					return item;
				})
				
				this.cartList = this.cartList.concat(list);
				this.calcTotal();
			},
			//监听image加载完成
			onImageLoad(key, index) {
				this.$set(this[key][index], 'loaded', 'loaded');
			},
			//监听image加载失败
			onImageError(key, index) {
				this[key][index].image = '/static/mixmall/errorImage.jpg';
			},
			navToLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			//选中状态处理
			check(type, index) {
				if (type === 'item') {
					this.cartList[index].checked = !this.cartList[index].checked;
					this.oneSelected(this.cartList[index], this.cartList[index].checked);

				} else {
					const checked = !this.allChecked;
					const list = this.cartList;
					list.forEach(item => {
						item.checked = checked;
					})
					this.allChecked = checked;
					this.allSelected();
				}
				this.calcTotal(type);
			},
			//数量
			numberChange(data, index) {

				this.cartList[index].count = data;
				this.calcTotal();
			},
			//删除
			deleteCartItem(index) {
				uni.showModal({
					content: '确认要删除吗？',
					success: (res) => {
						if (res.confirm) {
							let list = this.cartList;
							let row = list[index];
							let id = row.id;

							this.cartList.splice(index, 1);
							this.calcTotal();
							uni.hideLoading();
						}
					}
				})
			},
			//清空
			clearCart() {
				uni.showModal({
					content: '清空购物车？',
					success: (e) => {
						if (e.confirm) {
							this.cartList = [];
						}
					}
				})
			},
			//计算总价
			calcTotal() {
				let list = this.cartList;
				if (list.length === 0) {
					this.empty = true;
					return;
				}
				let total = 0;
				let checked = true;
				list.forEach(item => {
					if (item.checked === true) {
						console.log("item.count: ", item.count);
						total += item.price * item.count;

					} else if (checked === true) {
						checked = false;
					}
				})
				this.allChecked = checked;
				this.total = Number(total.toFixed(2));
			},
			//创建订单
			async createOrder() {
				const list = this.cartList;
				let checked = false;
				list.forEach(item => {
					checked = item.checked;
				})

				if (!checked) {
					this.$api.msg('请选择商品后结算');
					return;
				}

				uni.showLoading({
					title: '提交订单中...',
					mask: true
				})
				
				let res = await this.helper.requestAPI(this.api.check);
				let cartGoodsStatus = res.body.cartGoodsStatus;
				if (cartGoodsStatus == 200) {
					this.sumitOrder();
				}
			},
			oneSelected(item, state) {
				let data = {
					"isCheck": state,
					"cardId": item.cartId
				}
				
				this.helper.requestAPI(this.api.onecheck,data);
			},
			allSelected() {
				let data = {
					"isCheck": this.allChecked
				}
				
				this.helper.requestAPI(this.api.allcheck,data);
			},
			async sumitOrder() {
				let res = await this.helper.requestAPI(this.api.step2);
				let orderInfo = res.body;
				uni.navigateTo({
					url: '/pages/order/createOrder?orderInfo=' + encodeURIComponent(JSON
						.stringify(orderInfo))
				})
			},
			navToDetailPage(goodsId) {
				uni.navigateTo({
					url: '/pages/product/product?id=' + goodsId
				});
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background: $page-color-base;
	}

	.container {
		padding-bottom: 134rpx;

		/* 空白页 */
		.empty {
			position: fixed;
			left: 0;
			top: 0;
			width: 100%;
			height: 100vh;
			padding-bottom: 100rpx;
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			background: #fff;

			image {
				width: 240rpx;
				height: 160rpx;
				margin-bottom: 30rpx;
			}

			.empty-tips {
				display: flex;
				font-size: $uni-font-size-sm+2rpx;
				color: $font-color-disabled;

				.navigator {
					color: $uni-color-primary;
					margin-left: 16rpx;
				}
			}
		}
	}

	/* 购物车列表项 */
	.cart-item {
		display: flex;
		position: relative;
		padding: 30rpx 40rpx;
		margin: 20rpx;
		border-radius: 10rpx;
		background-color: white;

		.store-name {
			font-size: $uni-font-size-base;
			margin-left: 20rpx;
		}

		.selected-item {
			display: flex;
			align-items: center;
			margin-right: 30rpx;

			.selected-item-box {
				height: 40rpx;
				width: 40rpx;
			}
		}

		.image-wrapper {
			width: 200rpx;
			height: 200rpx;
			flex-shrink: 0;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
			}
		}

		.checkbox {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			left: -16rpx;
			top: -16rpx;
			z-index: 8;
			font-size: 44rpx;
			/* line-height: 1; */
			padding: 4rpx;
			color: $font-color-disabled;
			background: #fff;
			border-radius: 50rpx;
		}

		.item-right {
			display: flex;
			flex-direction: column;
			flex: 1;
			overflow: hidden;
			position: relative;
			padding-left: 30rpx;

			.title,
			.price {
				font-size: $uni-font-size-base;
				color: $font-color-dark;
				height: 40rpx;
				line-height: 40rpx;
				position: relative;
			}

			.title {
				overflow: hidden;
				-webkit-line-clamp: 1;
				text-overflow: clip;
				display: -webkit-box;
				-webkit-box-orient: vertical;
			}

			.attr {
				font-size: $uni-font-size-sm;
				color: $font-color-light;
				height: 50rpx;
				line-height: 50rpx;
			}

			.price {
				margin-top: 15rpx;
				height: 50rpx;
				line-height: 50rpx;
			}

			.step {
				position: absolute;
				bottom: 0;

				uni-number-box {
					transform: translateX(150rpx) scale(.6);
				}
			}
		}

		.del-btn {
			padding: 4rpx 10rpx;
			font-size: 34rpx;
			height: 50rpx;
			width: 50rpx;
			color: $font-color-light;
		}
	}

	/* 底部栏 */
	.action-section {
		/* #ifdef H5 */
		margin-bottom: 100rpx;
		/* #endif */
		position: fixed;
		left: 30rpx;
		bottom: 30rpx;
		z-index: 95;
		display: flex;
		align-items: center;
		width: 690rpx;
		height: 100rpx;
		padding: 0 30rpx;
		background: rgba(255, 255, 255, .9);
		box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, .5);
		border-radius: 16rpx;

		.checkbox {
			height: 52rpx;
			position: relative;

			image {
				width: 52rpx;
				height: 100%;
				position: relative;
				z-index: 5;
			}
		}

		.clear-btn {
			position: absolute;
			left: 26rpx;
			top: 0;
			z-index: 4;
			width: 0;
			height: 52rpx;
			line-height: 52rpx;
			padding-left: 38rpx;
			font-size: $uni-font-size-base;
			color: #fff;
			background: $font-color-disabled;
			border-radius: 0 50px 50px 0;
			opacity: 0;
			transition: .2s;

			&.show {
				opacity: 1;
				width: 120rpx;
			}
		}

		.total-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			text-align: right;
			padding-right: 40rpx;

			.price {
				font-size: $uni-font-size-lg;
				color: $font-color-dark;
			}

			.coupon {
				font-size: $uni-font-size-sm;
				color: $font-color-light;

				text {
					color: $font-color-dark;
				}
			}
		}

		.confirm-btn {
			padding: 0 38rpx;
			margin: 0;
			border-radius: 100rpx;
			height: 76rpx;
			line-height: 76rpx;
			font-size: $uni-font-size-base + 2rpx;
			background: $uni-color-primary;
			box-shadow: 1rpx 2rpx 5rpx rgba(217, 60, 93, 0.72)
		}
	}

	/* 复选框选中状态 */
	.action-section .checkbox.checked,
	.cart-item .checkbox.checked {
		color: $uni-color-primary;
	}
</style>
